/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.live.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.live.domain.Live;
import com.fzhucc.modules.live.domain.dto.LiveAddDto;
import com.fzhucc.modules.live.domain.dto.LiveDto;
import com.fzhucc.modules.live.domain.dto.LiveEditDto;
import com.fzhucc.modules.live.service.LiveService;
import com.fzhucc.modules.live.domain.dto.LiveQueryCriteria;
import com.fzhucc.modules.notice.domain.dto.NoticeDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-04-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "助企行:直播模块管理")
@RequestMapping("/api/live")
public class LiveController {

    private final LiveService liveService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('live:list')")
    public void exportLive(HttpServletResponse response, LiveQueryCriteria criteria) throws IOException {
        liveService.download(liveService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询助企行直播模块")
    @ApiOperation("查询助企行直播模块")
    @PreAuthorize("@el.check('live:list')")
    public ResponseEntity<Object> queryLive(LiveQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(liveService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询直播")
    @ApiOperation("h5、小程序查询直播")
    public ResponseEntity<Object> list(LiveQueryCriteria criteria, Pageable pageable){
        criteria.setIsShow(1);
        return new ResponseEntity<>(liveService.queryAllh5(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增助企行直播模块")
    @ApiOperation("新增助企行直播模块")
    @PreAuthorize("@el.check('live:add')")
    public ResponseEntity<Object> createLive(@Validated @RequestBody LiveAddDto resources){
        return new ResponseEntity<>(liveService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改助企行直播模块")
    @ApiOperation("修改助企行直播模块")
    @PreAuthorize("@el.check('live:edit')")
    public ResponseEntity<Object> updateLive(@Validated @RequestBody LiveEditDto resources){
        liveService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除助企行直播模块")
    @ApiOperation("删除助企行直播模块")
    @PreAuthorize("@el.check('live:del')")
    public ResponseEntity<Object> deleteLive(@RequestBody Integer[] ids) {
        liveService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个直播")
    @ApiOperation("查询单个直播")
    public LiveDto queryNoticeById(@PathVariable Integer id) {
        return liveService.findById(id);
    }
}